#include <iostream>
#include <functional>

using std::cout;
using std::endl;
using std::bind;

int add(int x, int y)
{
    cout << "int add(int, int)" << endl;
    return x + y;
}

int multiply(int x, int y, int z)
{
    cout << "int multiply(int, int, int)" << endl;
    return x * y * z;
}

class Example
{
public:
    int add(int x, int y)
    {
        cout << "int Example::add(int, int)" << endl;
        return x + y;
    }
};

void test0(){
    /* 在 C++ 中,函数名本身就是其地址。 */
    /* 如果要获取成员函数的地址， */
    /* 需要使用 `&Example::add` 这样的语法。 */
    auto f = bind(add,2,3);
    cout << "f()= " << f() << endl;
    cout << endl;

    auto f1 = bind(multiply,2,3,5);
    cout << "f1()= " << f1() << endl;
    cout << endl;

    Example ex;
    auto f2 = bind(&Example::add,&ex,3,4);
    cout << "f2()= " << f2() << endl;
    cout << endl;

    using namespace std::placeholders;
    auto f3 = bind(add,_6,_5);
    cout << "f(3)= " << f3(1,2,3,4,5,6,7) << endl;
}

int main(int argc, char * argv[])
{
    test0();
    return 0;
}


